<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermi="['jihuo:traffic:edit']"  icon="el-icon-edit" plain size="mini" type="success" @click="rechargeTraffic">
          充值
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="trafficList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column  key="jhTmAccountId" align="center" label="充值店铺" prop="jhTmAccountId">
        <template slot-scope="scope">
          <el-tag size="small">{{ jhTmAccountList.find(x => x.jhId == scope.row.jhTmAccountId) ? jhTmAccountList.find(x => x.jhId == scope.row.jhTmAccountId).jhName : '' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="充值流量数" prop="jhNum"/>
      <el-table-column align="center" label="充值时间" prop="jhUpdatedTime"/>
    </el-table>

    <pagination v-show="total>0" :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNum" :total="total" @pagination="getList"/>
    <!-- 充值对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="店铺名称" prop="jhTmAccountId">
          <el-select v-model="form.jhTmAccountId" filterable placeholder="请输入">
            <el-option v-for="item in jhTmAccountList" :key="item.jhId" :label="item.jhName" :value="item.jhId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="流量兑换码" prop="jhTraffic">
          <el-input v-model="form.jhTraffic" placeholder="请输入流量兑换码"/>
        </el-form-item>
        <el-form-item label="流量数" prop="jhNum">
          <el-input v-model="form.jhNum" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getCurrentUserTrafficRecord,rechargeTraffic} from "@/api/jihuo/sysTrafficRecord";
// todo 暂不考虑翻页与搜索问题
import {listAccount} from "@/api/jihuo/account";

export default {
  name: "Traffic",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 流量兑换码
      trafficList: [],
      // 店铺名称
      jhTmAccountList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        jhTmAccountId: [
          {required: true, message: "天猫账号不能为空", trigger: "blur"}
        ],
        jhTraffic: [
          {required: true, message: "店铺名称不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询已兑换流量记录 */
    getList() {
      this.loading = true;
      getCurrentUserTrafficRecord(this.queryParams).then(response => {
        this.trafficList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      listAccount().then(response => {
        this.jhTmAccountList = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        jhId: null,
        jhRevision: null,
        jhCreatedBy: null,
        jhCreatedTime: null,
        jhUpdatedBy: null,
        jhUpdatedTime: null,
        jhTmAccountList: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.jhId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 充值操作 */
    // todo 暂不回显加密串
    rechargeTraffic(row) {
      this.reset();
      const jhId = row.jhId || this.ids
      listAccount().then(response => {
        this.jhTmAccountList = response.rows;
        this.open = true;
        this.title = "流量充值";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        rechargeTraffic(this.form).then(response => {
          this.$modal.msg(response.msg);
          this.open = false;
          this.getList();
        });
      });
    },
  }
};
</script>
